﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Webcam.Image
{
    public struct RGB
    {
        /// <summary>Red</summary>
        byte _red;
        /// <summary>Green</summary>
        byte _green;
        /// <summary>Blue</summary>
        byte _blue;

        /// <summary>Red</summary>
        public byte red { get { return _red; } }
        /// <summary>Green</summary>
        public byte green { get { return _green; } }
        /// <summary>Blue</summary>
        public byte blue { get { return _blue; } }


        /// <summary>
        /// Get/Set the image as a grayscale img
        /// </summary>
        public byte GrayScale
        {
            get
            {
                if (IsGrayScale)
                    return _red;

                double val = 0.3 * _red + 0.59 * _green + 0.11 * _blue;
                return (byte)(int)val;
            }
            set
            {
                this._red = value;
                this._green = value;
                this._blue = value;
                IsGrayScale = true;
            }
        }

        /// <summary>
        /// Create a RBG object
        /// </summary>
        /// <param name="R">Red</param>
        /// <param name="G">Green</param>
        /// <param name="B">Blue</param>
        public RGB(byte R, byte G, byte B)
        {
            _red = R;
            _green = G;
            _blue = B;
            _isGrayScale = false;
        }

        /// <summary>
        /// Create a grayscale color
        /// </summary>
        /// <param name="Gray">The grayscale value</param>
        public RGB(byte Gray)
        {
            _red = Gray;
            _green = Gray;
            _blue = Gray;
            _isGrayScale = true;
        }

        /// <summary>
        /// Is this px a grayscale px
        /// </summary>
        bool _isGrayScale;

        /// <summary>
        /// Is this px a grayscale px
        /// </summary>
        public bool IsGrayScale
        {
            get { return _isGrayScale; }
            private set { _isGrayScale = true; }
        }

        /// <summary>
        /// Get the difference between the 2 RGB colors
        /// </summary>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        /// <returns>difference between 0 and byte.MaxValue*3</returns>
        public static int Difference(RGB c1, RGB c2)
        {
            int b;
            if (c1._blue < c2._blue)
                b = c2._blue - c1._blue;
            else
                b = c1._blue - c2._blue;

            int r;
            if (c1._red < c2._red)
                r = c2._red - c1._red;
            else
                r = c1._red - c2._red;

            int g;
            if (c1._green < c2._green)
                g = c2._green - c1._green;
            else
                g = c1._green - c2._green;

            int t = b + r + g;

            return t;
        }



        public static RGB Red = new RGB(byte.MaxValue, 0, 0);
        public static RGB Green = new RGB(0, byte.MaxValue, 0);
        public static RGB Blue = new RGB(0, 0, byte.MaxValue);
        public static RGB White = new RGB(byte.MaxValue, byte.MaxValue, byte.MaxValue);
        public static RGB Black = new RGB(0, 0, 0);
    }
}
